using System.Collections.Generic;
using Bosphorus.Integration.Jira.Thor.Core.Model.Domain;
using Bosphorus.Integration.Jira.Thor.Core.View.Domain;
using Bosphorus.Integration.Jira.Thor.Dal.Dao.Model;
using Bosphorus.Library.Dao.Core.Model.Vao;
using Bosphorus.Library.Dao.Facade;
using NQuery;
using NQuery.Extension.Executer;

namespace Bosphorus.Integration.Jira.Thor.Dal.Vao.Model
{
    public class IssueVao : AbstractVao<IssueView>
    {
        private const string NQUERY_SQL_SELECT = @"
            select 
                    Issue.Key, Issue.Summary, Issue.Reporter, Issue.Assignee,
                    IssueType.Name as IssueType,
                    IssueStatus.Name as IssueStatus     
            from 
                    Issue
                    inner join IssueType on IssueType.ID = Issue.Type
                    inner join IssueStatus on IssueStatus.ID = Issue.Status
            order by 
                    Issue.ID
       ";

        public IList<IssueView> GetByProject(string projectKey)
        {
            IList<Issue> issues = Repository.Domain.Live<Issue>.As<IssueDao>().GetByProject(projectKey);
            IList<IssueType> issueTypes = Repository.Domain.Cache<IssueType>.GetAll();
            IList<IssueStatus> issueStatuses = Repository.Domain.Cache<IssueStatus>.GetAll();

            DataContext dataContext = new DataContext();
            dataContext.Tables.Add(issues, "Issue");
            dataContext.Tables.Add(issueTypes, "IssueType");
            dataContext.Tables.Add(issueStatuses, "IssueStatus");

            Query query = new Query(dataContext);
            query.Text = NQUERY_SQL_SELECT;

            IList<IssueView> list = QueryExecuter.Execute(query).ToModel<IssueView>();
            return list;
        }
    }
}